package com.csu.tingtian.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

import java.io.Serializable;

@Getter
//json序列化时不包含为null的数据
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> implements Serializable {

    private int status;
    private String msg;
    private T data;
    private String token;
    private String[] roles;


    /***构造函数***/
    //只传一个状态码
    private CommonResponse(int status) {
        this.status = status;
    }
    //传状态码和msg
    private CommonResponse(int status,String msg) {
        this.status = status;
        this.msg = msg;
    }
    //传状态码、token、msg、data
    private CommonResponse(int status,String token,String msg,T data) {
        this.status = status;
        this.token = token;
        this.msg = msg;
        this.data = data;
    }

    //传状态码、roles
    private CommonResponse(int status,String[] roles) {
        this.status = status;
        this.roles = roles;
    }


    //传状态码和data
    private CommonResponse(int status,T data) {
        this.status = status;
        this.data = data;
    }
    //传状态码、msg、data
    private CommonResponse(int status,String msg,T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    @JsonIgnore
    /*****是否响应成功****/
    public boolean isSuccess() {
        return this.status == ResponseCode.SUCCESS.getCode();
    }


    /***响应成功****/
    //只返回一个状态码
    public static <T> CommonResponse<T> createForSuccess() {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    //返回一个状态码 + 数据
    public static <T> CommonResponse<T> createForSuccess(T data) {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    //返回一个状态码+信息
    //改变方法名，不用重载，这样就不会有数据是String类型，而传给了msg的情况
    public static <T> CommonResponse<T> createForSuccessMessage(String msg) {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    //返回一个状态码+信息+数据
    public static <T> CommonResponse<T> createForSuccess(String msg,T data) {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    //返回token+msg+数据
    public static <T> CommonResponse<T> createForToken(String token,String msg,T data) {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),token,msg,data);
    }

    //返回roles
    public static <T> CommonResponse<T> createForRoles(String[] roles) {
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),roles);
    }

    /********/

    /****响应错误****/
    //返回一个状态码+信息
    public static <T> CommonResponse<T> createForError() {
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDescription());
    }

    //返回一个状态码+指定信息
    public static <T> CommonResponse<T> createForError(String msg) {
        return new CommonResponse<T>(ResponseCode.ERROR.getCode(),msg);
    }

    //返回指定状态码+指定信息
    public static <T> CommonResponse<T> createForError(int status,String msg) {
        return new CommonResponse<T>(status,msg);
    }


}
